package com.js.rtu.util;

/**
 * Created by wz on 2020/12/1.
 */

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;

/**
 * @className: ConfigMgr
 * @author: yejinmo
 * @date: 2019/6/3 21:18
 * @Description: ini文件读取工具类
 */
public class ConfigMgr {
    /**
     * 配置文件
     */
    private static final String STR_CONFIG_FILE = "SetInfo.ini";
    private static HashMap<String, Properties> sections = new HashMap<>();
    private static transient Properties properties;
    private static ConfigMgr sConfigMgr;
    private final Context mContext;

    private ConfigMgr(Context context) {
        mContext = context;
    }

    public static ConfigMgr getInstance(Context context) {
        if (null == sConfigMgr) {
            sConfigMgr = new ConfigMgr(context);
        }
        return sConfigMgr;
    }

    //    public String readProperties(String section, String key) {
    //        try {
    //            InputStream inputStream = mContext.getResources().getAssets().open(STR_CONFIG_FILE);
    //            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    //            read(reader);
    //            reader.close();
    //        } catch (Exception ex) {
    //            ex.printStackTrace();
    //        }
    //
    //        Properties p = sections.get(section);
    //
    //        if (p == null) {
    //            return null;
    //        }
    //
    //        return p.getProperty(key);
    //    }


    public synchronized List<String> readProperties(String section, String key) {
        try {
            BufferedReader reader = null;
            String path = (String) SPUtils.get(mContext, "path", "");

            if (path==null||path.equals("")) {
                InputStream inputStream = mContext.getResources().getAssets().open(STR_CONFIG_FILE);
                reader = new BufferedReader(new InputStreamReader(inputStream));
            } else {

                File _file = new File(path);
                if (_file.exists()) {
                    reader = new BufferedReader(new FileReader(_file));

                }
            }

            if (reader!=null){
                read(reader);
                reader.close();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        Properties p = sections.get(section);

        if (p == null) {
            return new ArrayList<>();

        }
        String data = p.getProperty(key);
        if (data.contains("[")) {
            data = data.replace("[", "");
        }
        if (data.contains("]")) {
            data = data.replace("]", "");
        }

        if (data.contains(",")) {
            String[] strs = data.split(",");
            return Arrays.asList(strs);
        } else {
            return new ArrayList<>();
        }

    }


    private void read(BufferedReader reader) throws IOException {
        String line;
        while ((line = reader.readLine()) != null) {
            parseLine(line);
        }
    }

    private void parseLine(String line) {
        line = line.trim();
        if (line.matches("\\[.*\\]")) {
            String section = line.replaceFirst("\\[(.*)\\]", "$1");
            properties = new Properties();
            sections.put(section, properties);
        } else if (line.matches(".*=.*")) {
            if (properties != null) {
                int i = line.indexOf('=');
                String name = line.substring(0, i);
                String value = line.substring(i + 1);
                properties.setProperty(name, value);
            }
        }
    }
}

